# coding: UTF-8
import re
import ahocorasick
from utils import read_from_txt

def make_AC(AC, word_set):
    for word in word_set:
        AC.add_word(word,word)
    return AC

def find_region(str, AC_KEYS):
    ans = 0
    for i, AC_KEY in enumerate(AC_KEYS):
        for item in AC_KEY.iter(str):
            ans += (i + 1)
            break
    return ans

def cut_sent(para):
    para = re.sub('([。！？\?])([^”’])', r"\1\n\2", para)  # 单字符断句符
    para = re.sub('(\.{6})([^”’])', r"\1\n\2", para)  # 英文省略号
    para = re.sub('(\…{2})([^”’])', r"\1\n\2", para)  # 中文省略号
    para = re.sub('([。！？\?][”’])([^，。！？\?])', r'\1\n\2', para)
    # 如果双引号前有终止符，那么双引号才是句子的终点，把分句符\n放到双引号后，注意前面的几句都小心保留了双引号
    para = para.rstrip()  # 段尾如果有多余的\n就去掉它
    # 很多规则中会考虑分号;，但是这里我把它忽略不计，破折号、英文双引号等同样忽略，需要的再做些简单调整即可。
    return para.split("\n")

def build_trie(path):
    dic = []
    for line in open(path, 'r', encoding='utf8'):
        dic.append(line.strip())
    print(dic)
    AC_KEY = ahocorasick.Automaton()
    for word in set(dic):
        AC_KEY.add_word(word, word)
    AC_KEY.make_automaton()
    return AC_KEY
    

if __name__ == "__main__":
    AC_KEYS = []
    AC_KEYS.append(build_trie('./data/region_dic/India.txt'))
    AC_KEYS.append(build_trie('./data/region_dic/SouthSea.txt'))
    AC_KEYS.append(build_trie('./data/region_dic/Taiwan.txt'))
    
    #逐句标注
    N = 4152
    n = 0
    f = open('./data/manual.txt', 'a')
    for line in open('./data/news.txt', 'r', encoding='utf8'):
        news = line
        b = find_region(news, AC_KEYS)
        skip = 0
        if b == 0:
            skip = 1
        sentences = cut_sent(news)
        flag = 0
        for sentence in sentences:
            if n < N:
                n += 1
                continue
            a = find_region(sentence, AC_KEYS) 
            if a or skip:
                f.write(sentence)
                f.write('\t')
                f.write(str(a))
                f.write('\n')
                n += 1
            else:
                print(sentence)
                label = input()
                if label == '7':
                    # delete
                    n += 1
                    continue
                elif label == '8':
                    # break
                    flag = 1
                    break
                elif label == '':
                    f.write(sentence)
                    f.write('\t')
                    f.write('0')
                    f.write('\n')
                else:
                    f.write(sentence)
                    f.write('\t')
                    f.write(label)
                    f.write('\n')
                n += 1
            print('sentence: ', n)
        if flag == 1:
            break
    print('N = ', n)
    f.close()
    